using System;
using System.Text.RegularExpressions;

namespace umlungu.engineX.pop3X {
	/// <summary>
	/// umlungu engineX blog Pop3 class.
	/// </summary>
	public class x_pop3parse
	{
		/// <summary>
		/// Local objects and properties
		/// </summary>
		private static string[] pop3x_lineUpperTypeString = { 
			"From",
			"To",
			"Subject",
			"Content-Type"
		};

		private static string[] pop3x_lineSubTypeString = {
			"Content-Type",
			"Content-Transfer-Encoding",
			"Content-Description",
			"Content-Disposition"
		};

		private static string[] pop3x_nextLineTypeString = {
			"name",
			"filename"
		};

		// Mapping to lineSubTypeString ...
		public const int ContentTypeType = 0;
		public const int ContentTransferEncodingType = 1;
		public const int ContentDescriptionType = 2;
		public const int ContentDispositionType = 3;

		// Mapping to nextLineTypeString ...
		public const int NameType = 0;
		public const int FilenameType = 1;

		// Non-string mappers ...
		public const int UnknownType = -99;
		public const int EndOfHeader = -98;
		public const int MultipartBoundaryFound = -97;
		public const int ComponentsDone = -96;

		public static string[] LineUpperTypeString {
			get { return pop3x_lineUpperTypeString; }
		}

		public static string[] LineSubTypeString {
			get { return pop3x_lineSubTypeString; }
		}

		public static string[] NextLineTypeString {
			get { return pop3x_nextLineTypeString; }
		}

		public static string From(string line) {
			return Regex.Replace(line ,@"^From:.*[ |<]([a-z|A-Z|0-9|\.|\-|_]+@[a-z|A-Z|0-9|\.|\-|_]+).*$" ,"$1");
		}

		public static string Subject(string line) {
			return Regex.Replace(line ,@"^Subject: (.*)$" ,"$1");
		}

		public static string To(string line) {
			return Regex.Replace(line ,@"^To:.*[ |<]([a-z|A-Z|0-9|\.|\-|_]+@[a-z|A-Z|0-9|\.|\-|_]+).*$" ,"$1");
		}

		public static string ContentType(string line) {
			return Regex.Replace(line ,@"^Content-Type: (.*)$" ,"$1");
		}

		public static string ContentTransferEncoding(string line) {
			return Regex.Replace(line ,@"^Content-Transfer-Encoding: (.*)$" ,"$1");
		}

		public static string ContentDescription(string line) {
			return Regex.Replace(line ,@"^Content-Description: (.*)$" ,"$1");
		}

		public static string ContentDisposition(string line) {
			return Regex.Replace(line ,@"^Content-Disposition: (.*)$" ,"$1");
		}

		public static bool IsMultipart(string line) {
			return Regex.Match(line,"^multipart/.*").Success;
		}

		public static string MultipartBoundary(string line) {
			return Regex.Replace(line ,"^.*boundary=[\"]*([^\"]*).*$" ,"$1");
		}

		public static string Name(string line) {
			return Regex.Replace(line, "^[ |	]+name=[\"]*([^\"]*).*$","$1");
		}

		public static string Filename(string line) {
			return Regex.Replace(line, "^[ |	]+filename=[\"]*([^\"]*).*$","$1");
		}

		public static int GetSubHeaderNextLineType(string line) {
			int lineType = x_pop3parse.UnknownType;

			for(int i=0; i<x_pop3parse.NextLineTypeString.Length; i++) {
				string match = x_pop3parse.NextLineTypeString[i];

				if( Regex.Match(line,"^[ |	]+"+match+"="+".*$").Success ) {
					lineType = i;
					break;
				}
				if( line.Length == 0 ) {
					lineType = x_pop3parse.EndOfHeader;
					break;
				}
			}

			return lineType;
		}

		public static int GetSubHeaderLineType(string line, string boundary) {
			int lineType = x_pop3parse.UnknownType;

			for(int i=0; i<x_pop3parse.LineSubTypeString.Length; i++) {
				string match = x_pop3parse.LineSubTypeString[i];

				if( Regex.Match(line,"^"+match+":"+".*$").Success ) {
					lineType = i;
					break;
				} else if( line.Equals("--"+boundary) ) {
					lineType = x_pop3parse.MultipartBoundaryFound;
					break;
				} else if( line.Equals("--"+boundary+"--") ) {
					lineType = x_pop3parse.ComponentsDone;
					break;
				}
				if( line.Length == 0 ) {
					lineType = x_pop3parse.EndOfHeader;
					break;
				}
			}

			return lineType;
		}
	}
}
